/*
 * @Author: ByWang
 * @Date: 2021-06-01 16:12:48
 * @LastEditTime: 2021-06-13 21:54:14
 * @LastEditors: Please set LastEditors
 * @Description: 计时器结构体
 * @FilePath: \finalHWTSU\src\Counter.h
 */
#if !defined(COUNTER_H)
#define COUNTER_H
#include <iostream>
#include <Windows.h>
#include "./LCD.h"
using namespace std;

struct Counter
{
    int currentTime; //当前时间
    int Time;        //总的倒计时时�?
    int LCDList[10]; //LCD显示所需要的数字
    LCD *lcd;
    Counter(LCD *_lcd, const int n = 0)
    {
        Time = n;
        currentTime = n;
        lcd = _lcd;
    }
    /**
     * @description: 计时器前进一步，调节显示等�?
     * @param {*}
     * @return {*}
     */
    int iSetTime(int n)
    {
        if (n < 0 || n > 999)
        {
            return -1;
        }
        Time = n;
        currentTime = n;
    }
    /**
     * @description: 检验输入是否符合标准，这里默认输入的字符数目不会多于10个，实现思路是检查每个字符的ascii是否在规定范围?
     * @param {char} *input
     * @return {*}
     */
    bool bInputIsRight(char *input)
    {

        for (int i = 0; i < 10; i++)
        {
            if (input[i] == 0)
                return true;

            if (!(input[i] >= '0' && input[i] <= '9'||(input[0]=='-'&&i==0)))
                return false;
        }
        return true;
    }
    /**
     * @description: 计时器前进函数，更新currentTime，转换LCD接口可以接受的数据，清空屏幕，进行显示。当计时即将结束的时候显示110 在2s后显示校训?
     * @param {*}
     * @return {*}
     */
    void vStep()
    {
        if (currentTime > 1)
        {
            --currentTime;
            vCTtoLCD(currentTime, 0);
            lcd->vClear();
            int index = 0;
            while (LCDList[index] != -1)
            {
                lcd->vSet(index, LCDList[index]);
                index++;
            }
            lcd->vPrintN(index);
            for (int i = 0; i < 10; ++i)
            {
                LCDList[i] = -1; // 清空LCDList
            }
        }
        else
        {
            currentTime = 0;
            system("cls");
            vCTtoLCD(110, 0);
            lcd->vClear();
            int index = 0;
            while (LCDList[index] != -1)
            {
                lcd->vSet(index, LCDList[index]);
                index++;
            }
            lcd->vPrintN(index);
            for (int i = 0; i < 10; ++i)
            {
                LCDList[i] = -1; // 清空LCDList
            }
            Sleep(2000);
            system("cls");
            lcd->vDisp();
            Sleep(2000);
        }
    }
    /**
     * @description: Current Time to LCD ,为了方便显示递归使得数字位数逆序�?-1 为截止位�?
     * @param {*}
     * @return {*}
     */
    int vCTtoLCD(int current, int index)
    {

        if (current == 0)
        {
            LCDList[index] = -1;
            return 0;
        }
        int i = vCTtoLCD(current / 10, index + 1); // 上一级返回的index
        LCDList[i] = current % 10;
        return i + 1;
    };
    /**
     * @description: 计时器运行函�?
     * @param {*}
     * @return {*}
     */
    void vRun()
    {
        system("cls");
        while (currentTime != 0)
        {
            vStep();
            Sleep(1000);
            system("cls");
        }
    }
};

#endif // COUNTER_H
